package com.idbk.chargestation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.bean.JsonChargeList;
import com.idbk.chargestation.bean.JsonPointPile;

import java.util.List;

public class ChargeListAdapter extends BaseAdapter {

	private List<JsonChargeList.ChargePileInfo> mList;
	private LayoutInflater mInflater;

	public ChargeListAdapter(Context context,
                             List<JsonChargeList.ChargePileInfo> list) {
		mList = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHold hold;
		if (convertView == null) {
			view = mInflater.inflate(R.layout.item_charge_pile_info, parent, false);
			hold = new ViewHold();
			hold.initView(view);
			view.setTag(hold);
		} else {
			view = convertView;
			hold = (ViewHold) view.getTag();
		}
		
		JsonChargeList.ChargePileInfo pileInfo = mList.get(position);
		hold.mTexPileNum.setText(pileInfo.getPileNum());
		hold.mTexPerformance.setText(pileInfo.getPerformance());
		hold.mTexPointName.setText(pileInfo.pileName);
		hold.mTexSn.setText(pileInfo.pileSn);
		hold.mTexType.setText(JsonPointPile.getPileType(pileInfo.pileType));
		hold.mTexState.setText(pileInfo.getState());
		
		return view;
	}

	private class ViewHold {
		private TextView mTexPileNum, mTexPerformance, mTexPointName;
		private TextView mTexSn, mTexType, mTexState;
		
		public void initView(View view) {
			mTexPileNum = (TextView) view.findViewById(R.id.pile_number);
			mTexPerformance = (TextView) view.findViewById(R.id.performance);
			mTexPointName = (TextView) view.findViewById(R.id.point_name);
			mTexSn = (TextView) view.findViewById(R.id.sn);
			mTexType = (TextView) view.findViewById(R.id.pile_type);
			mTexState = (TextView) view.findViewById(R.id.pile_state);
		}
	}

}
